<?php
/**
 * Created by program
 * Class AreaService
 * Description:地区服务类
 * @package app\common\service
 * Author:sunnier
 * Email:xiaoyao_xiao@126.com
 * 2020-11-18 14:36
 */
namespace app\common\service;

class AreaService extends BaseService
{
    protected $areaModel;

    public function __construct()
    {
        parent::__construct();
        $this->areaModel = model('common/Area');
    }

    /**
     * @param array $where
     * @param string $order
     * @return array
     * Description:获取省列表
     * Author:sunnier
     * Email:xiaoyao_xiao@126.com
     * 2020-11-18 14:36
     */
    public function getProvince($where=['pid'=>0],$order='id ASC'){
        $count=$this->areaModel->where($where)->count();
        if($count>0){
            $list=$this->areaModel
                ->field('id as value,title as name')->where($where)
                ->order($order)->select()->toArray();
            array_unshift($list,['name'=>'全部','value'=>0]);
            return [
                'status'=>1,
                'msg'=>'获取成功！',
                'data'=>$list
            ];
        }else{
            $list=[];
            return [
                'status'=>1,
                'msg'=>'没数据了',
                'data'=>$list
            ];
        }
    }

    /**
     * @param int[] $where
     * @param string $order
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * Description:根据省ID获取市ID
     * Author:sunnier
     * Email:xiaoyao_xiao@126.com
     * 2020-11-18 15:49
     */
    public function getCityByProvince($where=[],$order='id ASC'){
        if($where['pid']==0){
            $list=[
                ['name'=>'全部','value'=>0]
            ];
            return [
                'status'=>1,
                'msg'=>'没数据了',
                'data'=>$list
            ];
        }
        $count=$this->areaModel->where($where)->count();
        if($count>0){
            $list=$this->areaModel
                ->field('id as value,title as name')->where($where)
                ->order($order)->cache('city_province_'.$where['pid'])->select()->toArray();
            array_unshift($list,['name'=>'全部','value'=>0]);
            return [
                'status'=>1,
                'msg'=>'获取成功！',
                'data'=>$list
            ];
        }else{
            $list=[];
            return [
                'status'=>1,
                'msg'=>'没数据了',
                'data'=>$list
            ];
        }
    }
}